local ClientOHOH = _G.ClientOHOH;
local PolicyService = game:GetService("PolicyService")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local M = ClientOHOH.class.new(script,ClientOHOH.files.OHOHOLYBUIBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local LanguageHelper= _G.LanguageHelper
local Buff_Cfg=_G.CommonOHOH.config.Buff
local ConnectClass = _G.CommonOHOH.load.Connect();
local UIManager = ClientOHOH.load.UIManager();
function InitInsts(ui)
    ui.insts = {};
    --useBtn
    ui.insts.useOne = "ctrl.btn.one"
    ui.insts.useAll = "ctrl.btn.all"
    ui.insts.input = "ctrl.btn.input"
    ui.insts.inputUse = "ctrl.btn.inputUse"

    --potionList
    ui.insts.cell = "ctrl.cell"
    ui.insts.cellBox = "ctrl.cellbox"
    ui.insts.close = "ctrl.close"

    --potionInfo
    ui.insts.name = "ctrl.name"
    ui.insts.num = "ctrl.num"
    ui.insts.potionIcon = "ctrl.potionIcon"
    ui.insts.description = "ctrl.description"
    ui.insts.timeText = "ctrl.time"
    ui.insts.grade = "ctrl.grade"

    ui.insts.scGui = "ctrl.scGui"
end

function M.new(...)
    return M(...);
end

function M.ctor(self)
    InitInsts(self)
    M.super.ctor(self,ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.PotionsGui"))
    self.UsePotionsEvent = ConnectClass.new()
    --color
    self.insts.normal = self.main_inst.ctrl.textColor.normal
    self.insts.shiny = self.main_inst.ctrl.textColor.shiny
    self.insts.rainbow = self.main_inst.ctrl.textColor.rainbow
    self.insts.void = self.main_inst.ctrl.textColor.void
    self.insts.astral = self.main_inst.ctrl.textColor.astral
    self.insts.potionsList = {}
    self.insts.cell.Visible = false
    self.selectedPotion = nil
    self:AddConnect()
end

function M.AddConnect(self)
    
    CallBackManager:AddRbxConnect(self,self.insts.close.Activated,function()
		self:Hide()
	end)
    
    CallBackManager:AddRbxConnect(self,self.insts.useOne.Activated,function()
        if self.selectedPotion then
            self.UsePotionsEvent:Action(self.selectedPotion.id,1)
        end
    
	end)

    CallBackManager:AddRbxConnect(self,self.insts.useAll.Activated,function()
        if self.selectedPotion then
            local count = self.database:GetData("buff")[self.selectedPotion.id]
            self.UsePotionsEvent:Action(self.selectedPotion.id,count)
        end
	end)
    
    CallBackManager:AddRbxConnect(self,self.insts.inputUse.Activated,function()
        if self.selectedPotion then
            local count = tonumber(self.insts.input.Text)
            if count then
                self.UsePotionsEvent:Action(self.selectedPotion.id,count)
            end
        end
	end)

    --保证用户只输入数字
    self.insts.input:GetPropertyChangedSignal("Text"):Connect(function()
        local inputText = self.insts.input.Text
        -- 替换所有非数字字符
        local filteredText = inputText:gsub("[^%d]", "")
        if inputText ~= filteredText then
            self.insts.input.Text = filteredText
        end
    end)

    --如果上次关闭时没有选中的药水，则默认选中第一个
    self.BaseShowEvent:Connect(function()
        if not self.selectedPotion then
            if #self.insts.potionsList ~= 0 then
                self.selectedPotion = self.insts.potionsList[1]
                self.selectedPotion.selected.Visible = true
                local buffList = self.database:GetData("buff")
                local id = self.selectedPotion.id
                self:SetInfoPanel(Buff_Cfg[id],buffList[id])
            end
        end
    end)    

end

local cellStr = {
    cell = "ctrl.cell",
    btn = "ctrl.btn",
    icon = "ctrl.icon",
    num = "ctrl.num",
    selected = "ctrl.selected",
    Normal = "ctrl.back.Normal",
    Shiny = "ctrl.back.Shiny",
    Rainbow = "ctrl.back.Rainbow",
    Void = "ctrl.back.Void",
    Astral = "ctrl.back.Astral"
}

--设置cell的背景
function M.InitBack(self,potion,grade)
    potion.Normal.Visible = false
    potion.Shiny.Visible = false
    potion.Rainbow.Visible = false
    potion.Void.Visible = false
    potion.Astral.Visible = false
    if grade == 1 then
        potion.Normal.Visible = true
    elseif grade == 2 then
        potion.Shiny.Visible = true
    elseif grade == 3 then
        potion.Rainbow.Visible = true
    elseif grade == 4 then
        potion.Void.Visible = true
    else
        potion.Astral.Visible = true
    end
end


function M.Init(self)
    local buffList = self.database:GetData("buff")
    local j = 1
    for i, value in pairs(buffList) do
        local index = tonumber(i)
        if value == 0 then
            continue
        end
        local potionObj = self.insts.cell:Clone()
        local potion = self:InitUiInst2(potionObj,cellStr)
        self:InitUiObj(potionObj)
        local grade = Buff_Cfg[index].grade
        self:InitBack(potion,grade)
        potion.num.Text = "x"..value
        potion.icon.Image = Buff_Cfg[index].icon
        potion.cell.Parent = self.insts.cellBox
        potion.cell.Visible = true
        potion.id = index
        --选中设置
        potion.selected.Visible = false
        if j==1 then
            if self.selectedPotion==nil then
                --默认第一个选中
                potion.selected.Visible = true
                self.selectedPotion = potion
                self:SetInfoPanel(Buff_Cfg[index],value)
                j = 0
            else
                --保持上次选中
                if self.selectedPotion.id == index then
                    potion.selected.Visible = true
                    self.selectedPotion = potion
                    self:SetInfoPanel(Buff_Cfg[index],value)
                    j = 0
                end
            end
        end
        --点击响应事件设置，显示选中，更改左侧详情
        CallBackManager:AddRbxConnect(self,potion.btn.Activated,function()
            if self.selectedPotion and self.selectedPotion~=potion then
                self.selectedPotion.selected.Visible = false
            end
            potion.selected.Visible = not potion.selected.Visible 
            if potion.selected.Visible then
                self.selectedPotion = potion
                self:SetInfoPanel(Buff_Cfg[index],value)
            else
                self.selectedPotion = nil
                self:SetInfoPanel()
            end
        end)
        table.insert(self.insts.potionsList,potion)
    end

end

--设置详情面板的显示信息，如果不传potionInfo则表示没有选中
function M.SetInfoPanel(self,potionInfo,num)
    local children = self.insts.grade:GetChildren()
    for _, child in pairs(children) do
        child:Destroy()
    end
    if  potionInfo then
        self.insts.name.Text = LanguageHelper:GetText(potionInfo.name)
        self.insts.num.Text = "x"..num
        self.insts.potionIcon.Image = potionInfo.icon
        self.insts.description.Text = LanguageHelper:GetText(potionInfo.description)
        self.insts.timeText.Text = potionInfo.time.."s"
        local grade = potionInfo.grade
        -- #todo:文字待修改
        self.insts.grade.Text = "传奇"
        local textColor = nil
        if grade == 1 then
            textColor = self.insts.normal:Clone()
        elseif grade == 2 then
            textColor = self.insts.shiny:Clone()
        elseif grade == 3 then
            textColor = self.insts.rainbow:Clone()
        elseif grade == 4 then
            textColor = self.insts.void:Clone()
        else
            textColor = self.insts.astral:Clone()
        end
            textColor.Parent = self.insts.grade
    else
        self.insts.name.Text = "??????"
        self.insts.num.Text = "?"
        self.insts.potionIcon.Image = ""
        self.insts.description.Text = "???"
        self.insts.timeText.Text = "???"
        self.insts.grade.Text = "???"
        local textColor = self.insts.normal:Clone()
        textColor.Parent = self.insts.grade
    end
end



--清除，销毁创建的cell，清空list
function M.Clear(self)
    --如果增加或者使用药水后数量大于0则保持选中对象
    if not self.selectedPotion then
        return   
    end
    local count = self.database:GetData("buff")[self.selectedPotion.id]
    if count==0 or count ==nil then
        self.selectedPotion = nil
    end
    if self.insts.potionsList then
        for _, potion in pairs(self.insts.potionsList) do
            potion.cell:Destroy()
        end
        self.insts.potionsList = {}
        self:SetInfoPanel()
    end
end


function M.SetDatabase(self,database)
    self.database = database
end


function M.dtor(self)
    M.super.dtor(self);
end

return M